const apppackage = require("../../package.json")
import mitt from 'mitt'
export function dateFormat(d, fmt = 'yyyy-MM-dd hh:mm:ss') {
	if (typeof d == 'string') {
		if (d.length == 10) d = d + '000'
		if (!isNaN(d)) d = Number(d)
	}
	if (typeof d == 'number') {
		if (d.toString().length == 10) d = d * 1000
	}
	if (!(d instanceof Date)) {
		d = new Date(d)
	}
	var o = {
		"M+": d.getMonth() + 1, //月份
		"d+": d.getDate(), //日
		"h+": d.getHours(), //小时
		"m+": d.getMinutes(), //分
		"s+": d.getSeconds(), //秒
		"q+": Math.floor((d.getMonth() + 3) / 3), //季度
		"S": d.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}
/**
 * 生成随机字符串
 * @param {number} len 字符串长度
 * @return {string}
 */
export function randomString(len) {
	len = len || 32;
	var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
	var maxPos = $chars.length;
	var pwd = '';
	for (let i = 0; i < len; i++) {
		pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
	}
	return pwd;
}

/**
 * 创建oss路径
 * @param {string} data 文件base64/file
 * @param {string} folder 上传到oss的目录
 */
export function createOssPath(data, folder = '') {
	let type
	let file
	let year = dateFormat(new Date(), 'yyyy')
	let month = dateFormat(new Date(), 'MM')
	let day = dateFormat(new Date(), 'dd')
	let name = `${year}/${month}/${day}/${randomString(16)}`
	if (typeof data == 'string') {
		type = data.slice(data.indexOf('/') + 1, data.indexOf(';'))
		file = dataURLtoFile(data, fileName)
	}
	if (typeof data == 'object') {
		type = data.path.split('.')
		type = type[type.length - 1]
		file = data
	}
	if (folder && folder.charAt(folder.length - 1) !== '/') {
		folder = folder + '/'
	}
	let fileName = `${apppackage.name}/assets/${process.env.VUE_APP_outputDir}/` + folder + name + '.' + type

	return {
		fileName,
		file
	}
}

/**
 * 深拷贝
 * @param {object} data
 */
export function deepClone(data) {
	function type(obj) {
		var toString = Object.prototype.toString;
		var map = {
			'[object Boolean]': 'boolean',
			'[object Number]': 'number',
			'[object String]': 'string',
			'[object Function]': 'function',
			'[object Array]': 'array',
			'[object Date]': 'date',
			'[object RegExp]': 'regExp',
			'[object Undefined]': 'undefined',
			'[object Null]': 'null',
			'[object Object]': 'object'
		};
		return map[toString.call(obj)];
	}
	var t = type(data),
		o, i, ni;

	if (t === 'array') {
		o = [];
	} else if (t === 'object') {
		o = {};
	} else {
		return data;
	}

	if (t === 'array') {
		for (i = 0, ni = data.length; i < ni; i++) {
			o.push(deepClone(data[i]));
		}
		return o;
	} else if (t === 'object') {
		for (i in data) {
			o[i] = deepClone(data[i]);
		}
		return o;
	}
}

/**
 * 检测路由权限
 * @param {object} route
 */
export function checkAccess(route,user){
	if(user.userType=='1'){
		return true
	}else if(route.meta.noaccess){
		return true
	}else{
		return user.access.includes(route.name)
	}
}

/**
 * 获取文件后缀名
 */
export function getExtname(filename){
	if(!filename||typeof filename!='string'){
	   return false
	};
	let a = filename.split('').reverse().join('');
	let b = a.substring(0,a.search(/\./)).split('').reverse().join('');
	return b
};


/**
 * eventBus
 */
export var eventBus=mitt()
